package com.web.controller.lxf;

import com.core.dto.RoleDTO;
import com.core.model.user.Privilege;
import com.service.user.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/6/27
 * Time: 17:13
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "form", method = RequestMethod.GET)
    public ModelAndView roleAddOrUpdateIndex(Model model) {
        model.addAttribute("privilegesEnum", Privilege.toMap());
        return new ModelAndView("back/user/roleUpdate", "roleDTO", new RoleDTO());
    }

    @RequestMapping(value = "form/{roleGuid}", method = RequestMethod.GET)
    public ModelAndView roleUpdateIndex(@PathVariable String roleGuid, Model model) {
        RoleDTO roleDTO = roleService.findRoleByGuid(roleGuid);
        model.addAttribute("privilegesEnum", Privilege.toMap());
        return new ModelAndView("back/user/roleUpdate", "roleDTO", roleDTO);
    }

    @RequestMapping(value = "form", method = RequestMethod.POST)
    public String roleAddOrUpdate(RoleDTO roleDTO) {
        roleService.saveOrUpdateRole(roleDTO);
        return "redirect:/role/list";
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String roleListIndex() {
        return "back/user/role";
    }

    @RequestMapping(value = "list/{currentPage}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> roleList(@PathVariable String currentPage) {
        return roleService.roleList(currentPage);
    }

    @RequestMapping(value = "del/{roleGuid}", method = RequestMethod.GET)
    @ResponseBody
    public boolean delRole(@PathVariable String roleGuid) {
        return roleService.delRoleByGuid(roleGuid);
    }


}
